#define _LED_MODULE_

#include "common.h"
#include "gpio.h"
#include "ledctr.h"
#include "pulse.h"
#include "dataproc.h"
#include "FreeRTOS.h"
#include "task.h"
#include "pulse.h"

const unsigned char  numtab[16]= {0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x90,0x88,0x83,0xC6,0xA1,0x86,0x8E};
const uint8_t AsciiCode[] =
{
    0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40,
    0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40,                                                            -
    0x00, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40,
    //0   1     2     3     4     5     6     7     8     9      A    B     C      D    E     F
    0x3f, 0x06, 0x5b, 0x4f, 0x66, 0x6d, 0x7d, 0x07, 0x7f, 0x6f, 0x77, 0x7c, 0x39, 0x5e, 0x79, 0x71, 
    //@   A     B     C     D     E     F     G     H     I     J     K     L     M     N     O
    0x40, 0x77, 0x7c, 0x39, 0x5e, 0x79, 0x71, 0x3D, 0x76, 0x30, 0x0E, 0x7A, 0x38, 0x55, 0x37, 0x3f,
    //P   Q     R     S     T     U     V     W     X     Y     Z
    0x73, 0x67, 0x77, 0x6d, 0x31, 0x3E, 0x3E, 0x6A, 0x76, 0x6E, 0x5b, 0x40, 0x40, 0x40, 0x40, 0x40,
    //.   a     b     c     d     e     f     g     h     i     j     k     l     m     n     o
    0x80, 0x77, 0x7c, 0x58, 0x5e, 0x7B, 0x71, 0x6f, 0x74, 0x30, 0x0E, 0x7A, 0x38, 0x55, 0x54, 0x5C,
    //p   q     r     s     t     u     v     w     x     y     z
    0x73, 0x67, 0x50, 0x6d, 0x78, 0x1C, 0x1C, 0x6A, 0x76, 0x6E, 0x5b, 0x40, 0x40, 0x40, 0x40, 0x40,
};

uint8_t LedBuff[4];
uint32_t LedCnt =0;
uint8_t databuff[6];

timer_pulse_type led_pulse;

/*****************************************************************************//*!
*
* @brief   led task.
*
* @param   none
*
* @return  none
*
* @ Pass/ Fail criteria: none
*****************************************************************************/
void LedCtrTask(void *argument)
{
    led_ctr_init();
    pulse_init(&led_pulse, uTick);
    
    for(;;)
    {
        PulseCreat(&led_pulse, uTick);
        
        if(led_pulse.bPulse_1s)
        {
            LedCnt++;
            Uint16ToAscii(LedCnt,3,databuff);
            
            LedBuff[0]=AsciiCode[databuff[0]];
            LedBuff[1]=AsciiCode[databuff[1]];
            LedBuff[2]=AsciiCode[databuff[2]];
            LedBuff[3]=AsciiCode[databuff[3]];
        }
        
        vTaskDelay( 200 );
    }
}

/*****************************************************************************//*!
*
* @brief   led init.
*
* @param   none
*
* @return  none
*
* @ Pass/ Fail criteria: none
*****************************************************************************/
void led_ctr_init(void)
{
    GPIO_Open(GPIOH, DIR_OUTPUT, PULL_UP);
}

/*****************************************************************************//*!
*
* @brief   led handle.
*
* @param   none
*
* @return  none
*
* @ Pass/ Fail criteria: none
*****************************************************************************/
void led_ctr_scan(void)
{
    static uint16_t i;
    uint32_t SegMsk;
    
    i++;
    i&=0x03;
    SegMsk = 0x0FUL& (~(1UL<<i));
    SegMsk <<= 12;

    outpw(REG_GPIOA_DATAOUT + GPIOH,(((~LedBuff[i])&0xFF)<<4) | SegMsk);
}


